/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package php;

import define.AttributeDefine;
import define.ClassDefine;
import define.CreateTpl;
import define.PartialDefine;
import define.ProjectDefine;
import define.TemplateDefine;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import source.AttributeSource;
import source.ClassSource;
import source.ParamSource;
import source.ProjectSource;

/**
 *
 * @author RoderickNestor
 */
public class Test {

    public void create_define() {
        ProjectDefine project = new ProjectDefine();

        ClassDefine cls_scaffold = new ClassDefine();
        cls_scaffold.name = "Scaffold";
        cls_scaffold.params = "caption";

        AttributeDefine attr_col = new AttributeDefine();
        attr_col.name = "addField";
        attr_col.params = "name,type,caption";

        AttributeDefine attr_foregin = new AttributeDefine();
        attr_foregin.name = "addForegin";
        attr_foregin.params = "name,caption,refences,field";

        cls_scaffold.atrrs.add(attr_col);
        cls_scaffold.atrrs.add(attr_foregin);
        //----------------------------------------------------------------------
        TemplateDefine tpl_sql = new TemplateDefine();
        tpl_sql.name = "sql";
        tpl_sql.path = "sql.tpl";
        tpl_sql.filename = "sqls/${a_singularize_low(class_name)}.sql";

        PartialDefine tpl_sql_attr = new PartialDefine();
        tpl_sql_attr.name = "sql_attr";
        tpl_sql_attr.path = "sql_attr.tpl";
        tpl_sql_attr.tpl = "sql";

        cls_scaffold.templates.add(tpl_sql);
        cls_scaffold.partials.add(tpl_sql_attr);
        //----------------------------------------------------------------------
        /*TemplateDefine tpl_model = new TemplateDefine();
        tpl_model.name = "model";
        tpl_model.path = "model.tpl";
        tpl_model.filename = "models/${a_singularize_cap(class_name)}.php";

        PartialDefine tpl_model_attr = new PartialDefine();
        tpl_model_attr.name = "model_attr";
        tpl_model_attr.path = "model_attr.tpl";
        tpl_model_attr.tpl = "model";

        cls_scaffold.templates.add(tpl_model);
        cls_scaffold.partials.add(tpl_model_attr);
        //----------------------------------------------------------------------
        TemplateDefine tpl_controller = new TemplateDefine();
        tpl_controller.name = "controller";
        tpl_controller.path = "controller.tpl";
        tpl_controller.filename = "controllers/${a_pluralize_cap(class_name)}.php";

        cls_scaffold.templates.add(tpl_controller);
        //----------------------------------------------------------------------
        TemplateDefine tpl_view_list = new TemplateDefine();
        tpl_view_list.name = "view_list";
        tpl_view_list.path = "view_list.tpl";
        tpl_view_list.filename = "views/${a_pluralize_low(class_name)}/index.php";

        cls_scaffold.templates.add(tpl_view_list);
        //----------------------------------------------------------------------
        TemplateDefine tpl_view_create = new TemplateDefine();
        tpl_view_create.name = "view_create";
        tpl_view_create.path = "view_create.tpl";
        tpl_view_create.filename = "views/${a_pluralize_low(class_name)}/create.php";

        cls_scaffold.templates.add(tpl_view_create);*/
        //----------------------------------------------------------------------

        //----------------------------------------------------------------------
        project.list.add(cls_scaffold);
        JAXBContext jc = null;
        String path_xml_out = "E:\\agile\\AgileLangVisual\\src\\php\\define.xml";
        Marshaller marshaller;
        java.io.StringWriter sw = new StringWriter();
        try {
            jc = JAXBContext.newInstance(ProjectDefine.class);
            marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(project, sw);

        } catch (JAXBException ex) {
            System.out.println("Error --> " + ex.toString());
        }
        System.out.println(sw.toString());
        Writer output = null;
        System.out.println(path_xml_out);
        File file = new File(path_xml_out);
        try {
            //output = new BufferedWriter(new FileWriter(file));

            output = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file), "UTF8"));

            output.write(sw.toString());

            output.flush();
            output.close();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }

        //----------------------------------------------------------------------
        //----------------------------------------------------------------------
        String path_xml = "E:\\agile\\AgileLangVisual\\src\\php\\define.xml";
        CreateTpl rd = new CreateTpl();
        rd.process(path_xml);
        rd.create_tpl("E:\\agile\\AgileLangVisual\\src\\php_tpl\\");
    }

    public void create_source() {
        ProjectSource project = new ProjectSource();

        ClassSource cls_scaffold = new ClassSource();
        cls_scaffold.name = "unit";
        cls_scaffold.type = "Scaffold";
        cls_scaffold.params.add(new ParamSource("caption", "Unidades"));

        AttributeSource attr_col = new AttributeSource();
        attr_col.name = "addField";
        attr_col.params.add(new ParamSource("name", "name"));
        attr_col.params.add(new ParamSource("type", "string"));
        attr_col.params.add(new ParamSource("caption", "Nombre"));
        
        cls_scaffold.atrrs.add(attr_col);
        //----------------------------------------------------------------------
        project.list.add(cls_scaffold);
        JAXBContext jc = null;
        String path_xml_out = "E:\\agile\\AgileLangVisual\\src\\php\\source.xml";
        Marshaller marshaller;
        java.io.StringWriter sw = new StringWriter();
        try {
            jc = JAXBContext.newInstance(ProjectSource.class);
            marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(project, sw);

        } catch (JAXBException ex) {
            System.out.println("Error --> " + ex.toString());
        }
        System.out.println(sw.toString());
        Writer output = null;
        System.out.println(path_xml_out);
        File file = new File(path_xml_out);
        try {
            //output = new BufferedWriter(new FileWriter(file));

            output = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file), "UTF8"));

            output.write(sw.toString());

            output.flush();
            output.close();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }

        //----------------------------------------------------------------------
        
    }

    public static void main(String args[]) {
        Test test = new Test();
        test.create_define();
        test.create_source();
    }
}
